clearvars
%close all

filePath = matlab.desktop.editor.getActiveFilename;
fprintf('%s\n',filePath);

repository_base = fileparts(fileparts(filePath));
addpath(fullfile(repository_base, "scripts"));
addpath(fullfile(repository_base, "scripts", "libs"));

session_list{1} = 'sub-A';
session_list{2} = 'sub-B';
session_list{3} = 'sub-C';
session_list{4} = 'sub-D';
session_list{5} = 'sub-E';
session_list{6} = 'sub-F';
session_list{7} = 'sub-G';
session_list{8} = 'sub-H';
session_list{9} = 'sub-I';
session_list{10} = 'sub-J';
session_list{11} = 'sub-K';

f_base = fullfile(repository_base, 'epochs');
save_base = fullfile(repository_base, 'results');

for session_idx = 1:length(session_list)
    session = session_list{session_idx}
    f_name = fullfile(f_base, strcat(session, '_filt_2_0.1_40.mat'));
    load(f_name);
    epochs_{session_idx} = epochs;
end

epochs = epochs_;

color = ['r' 'g' 'b'];
color_mesh = [0.7 0.7 0.7];

alpha_ttest = 0.01;

eeg_ch = 1:64;
eeg_th = [-100 100];

eog_ch = 65:66;
eog_th = [-500 500];

ch_idx = 32;

for session_idx = 1:8
    for idx_att = 1:3
        for idx_marker = 1:3
            epochs{session_idx}.att{idx_att}.marker{idx_marker}.rej_th(eeg_ch, eeg_th);
            epochs{session_idx}.att{idx_att}.marker{idx_marker}.rej_th(eog_ch, eog_th);
        end
    end
end

%%
for idx_att = 1:3
    for idx_marker = 1:3
        data.att{idx_att}.marker{idx_marker} = [];
    end
end

for session_idx = 1:8
    for idx_att = 1:3
        for idx_marker = 1:3
            data.att{idx_att}.marker{idx_marker} = cat(3, data.att{idx_att}.marker{idx_marker}, epochs{session_idx}.att{idx_att}.marker{idx_marker}.data);
        end
    end
end

%%
time = epochs{1}.att{1}.marker{1}.time;
size(data.att{1}.marker{1})
size(mean(data.att{1}.marker{1},3))

color = ['r' 'g' 'b'];

for dev = 1:3
    f = figure();
    
    t = [];
    nt = [];
    
    for att = 1:3
        data_plot = mean(data.att{att}.marker{dev},3);
        plot(time, data_plot(ch_idx,:),'color', color(att), 'linewidth', 3)
        hold on        
        if dev == att
            t = data.att{att}.marker{dev};
        else
            nt = cat(3, nt, data.att{att}.marker{dev});
        end
    end
    t = squeeze(t(ch_idx, :, :));
    nt = squeeze(nt(ch_idx, :, :));
    axis ij
    xlabel("Time [s]")
    ylabel("Potential [\muV]")
    set(gca,'FontSize',18)
    xlim([-0.1 0.5])
    xaxis = xlim;
    yaxis = ylim;
    plot([time(1) time(length(time))], [0 0], 'color','k','linestyle','-','linewidth',3)
    plot([0 0], [yaxis(1) yaxis(2)], 'color','k','linestyle','--','linewidth',3)
    
    % ttest    
    [h,p,ci,stats] = ttest2(t,nt,'Dim',2,'Alpha',alpha_ttest);
    plot_ttest(h, time, yaxis, color_mesh);

    for att = 1:3
        data_plot = mean(data.att{att}.marker{dev},3);
        plot(time, data_plot(ch_idx,:),'color', color(att), 'linewidth', 3)
        axis ij
    end

    exportgraphics(f, fullfile(save_base, strcat('grand_responses_to_',num2str(dev),'.png')), 'resolution', 300)
end